package com.example.bgmachine.bgmachine.databases;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseInfor extends SQLiteOpenHelper {
    public MyDatabaseInfor(Context context) {
        super(context, "ordertable.db", null, 1);
    }
    /**
     * 当数据库第一次调用时
     * 特别适合做表结构的初始化
     * @param sqLiteDatabase
     */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        //版本注册号
        sqLiteDatabase.execSQL("create table devicerigister( verNumber integer primary key ,versions varchar(160)  )");

        sqLiteDatabase.execSQL("create table speedcheacktb( pin integer primary key ,speed float DEFAULT 1 ,time float DEFAULT 0  )");
        sqLiteDatabase.execSQL("create table capacitytb( pin integer primary key ,surplus integer DEFAULT 0 ,capacity integer DEFAULT 0  )");
        sqLiteDatabase.execSQL("create table pininfor( pin integer primary key ,reala integer DEFAULT 0  )");

        sqLiteDatabase.execSQL("create table salesouts( ordernums varchar(500) primary key ,salesvolumeOrderTime  varchar(300) ,secondMenuid  varchar(300)  , cupTypeid varchar(300) , contents text )");
        sqLiteDatabase.execSQL("create table cleartimea( ordernums varchar(500) primary key , cleantimename varchar(300) , begin_time varchar(300), theoretical_time varchar(300)  )");
        sqLiteDatabase.execSQL("create table cleartimeb( ordernums varchar(500) primary key , cleantimename varchar(300) , begin_time varchar(300), theoretical_time varchar(300)  , over_time varchar(300) )");

//        sqLiteDatabase.execSQL("create table salesouts( ordernums varchar(500) primary key ,secondnames  varchar(300) ,devicenums varchar(300) , typenames varchar(300) , contents text )");
//        sqLiteDatabase.execSQL("create table cleartimes( ordernums varchar(500) primary key , devicenums varchar(300) , cleartime varchar(300)  )");


        //更新状态校验 speed-流速
        sqLiteDatabase.execSQL("create table outmaterials( ids INTEGER PRIMARY KEY AUTOINCREMENT, menuType integer   ,pin integer  ,weighs int  ,speed float DEFAULT 1 ,times int DEFAULT 0 ,nums integer  DEFAULT 0 )");
        sqLiteDatabase.execSQL("create table menumaterials( ids INTEGER PRIMARY KEY AUTOINCREMENT,sendMeus integer  , menuType integer   ,nums integer  DEFAULT 0 )");

        //记录清晰状况
        sqLiteDatabase.execSQL("create table cleanRecord( ids INTEGER PRIMARY KEY AUTOINCREMENT,cleanStartTime varchar(300)   ,cleanEndTime varchar(300) )");

        sqLiteDatabase.execSQL("create table pwds( verNumber integer primary key ,pwd varchar(160)  )");

    }

    /**
     * 用于数据库更新
     * @param sqLiteDatabase
     * @param i
     * @param i1
     */
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public static boolean checkAndCreateTable(SQLiteDatabase db, String tableName, String createTableSQL) {
        if (db == null || !db.isOpen()) {
            return false;
        }

        // 检查表是否存在
        if (isTableExists(db, tableName)) {
            return true;
        }

        // 表不存在，尝试创建
        try {
            db.execSQL(createTableSQL);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 检查表是否存在
     * @param db SQLiteDatabase对象
     * @param tableName 表名
     * @return true:存在; false:不存在或发生异常
     */
    private static boolean isTableExists(SQLiteDatabase db, String tableName) {
        if (db == null || !db.isOpen() || tableName == null || tableName.isEmpty()) {
            return false;
        }

        Cursor cursor = null;
        try {
            cursor = db.rawQuery(
                    "SELECT COUNT(*) FROM sqlite_master WHERE type = ? AND name = ?",
                    new String[]{"table", tableName}
            );

            if (cursor != null && cursor.moveToFirst()) {
                int count = cursor.getInt(0);
                return count > 0;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

}
